package org.example.aaaatest.money;

import com.alibaba.fastjson.JSONObject;
import com.amap.car.intl.diamond.ExchangeRateInfo;
import com.amap.car.intl.util.ExchangeRateUtil;
import com.amap.car.order.i18n.entity.MoneyExchangeRateParam;
import com.amap.car.order.i18n.entity.MoneyExchangeRateResponse;
import com.amap.car.order.i18n.enums.CurrencyEnum;
import com.amap.car.order.i18n.money.service.MoneyExchangeRateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

@Slf4j
/**
 * @description:
 * @author: wangbaogui
 * @create: 2025/4/10 13:42
 **/
public class a11 {
    public static void main(String[] args) {




        ExchangeRateDto exchangeRateDto = new ExchangeRateDto();
        //BigDecimal bd3 = BigDecimal.valueOf(123.45);
        String bd3 = null;
        String bigDecimal = Optional.ofNullable(bd3).orElse("不是空的");
        System.out.println("bigDecimal222222222222222222 = " + bigDecimal);
        System.out.println("bd3 = " + bd3);
        BigDecimal bd4 = new BigDecimal("233.34555");
        System.out.println("bd4 = " + bd4);
        exchangeRateDto.setCurrentCurrency("USD");
        exchangeRateDto.setCurrentMoneyValue(1.011);
        exchangeRateDto.setTargetCurrency("CNY");
        CurrencyConversion currencyConversion = recordLog(exchangeRateDto);
        System.out.println("currencyConversion = " + currencyConversion);


        CurrencyConversion currencyConversion1 = new CurrencyConversion();


    }

    public static CurrencyConversion recordLog(ExchangeRateDto exchangeRateDto) {

        ExchangeRateInfo rate = ExchangeRateUtil.getRate(exchangeRateDto.getCurrentCurrency(), exchangeRateDto.getTargetCurrency());
        System.out.println("rate = " + rate);

        if (exchangeRateDto == null ||
                StringUtils.isBlank(exchangeRateDto.getCurrentCurrency()) ||
                StringUtils.isBlank(exchangeRateDto.getTargetCurrency()) ||
                exchangeRateDto.getCurrentMoneyValue() == null ||
                exchangeRateDto.getCurrentMoneyValue() == 0) {
            System.out.println("exchangeRateDto2222222222222222222 = " + exchangeRateDto);
        }
        CurrencyConversion currencyConversion = new CurrencyConversion();
        //计算 转换的金额
        MoneyExchangeRateParam moneyExchangeRateParam = new MoneyExchangeRateParam();
        moneyExchangeRateParam.setExchangeRate(new BigDecimal(7.31));
        moneyExchangeRateParam.setCurrentCurrencyEnum(CurrencyEnum.getEnumByCurrency(exchangeRateDto.getCurrentCurrency()));
        moneyExchangeRateParam.setTargetCurrencyEnum(CurrencyEnum.getEnumByCurrency(exchangeRateDto.getTargetCurrency()));
        //换算成美元
        BigDecimal bigDecimal = BigDecimal.valueOf(exchangeRateDto.getCurrentMoneyValue()).setScale(2, RoundingMode.HALF_UP);
        moneyExchangeRateParam.setCurrentMoneyValue(bigDecimal.multiply(new BigDecimal("100")).intValue());
        log.info("CurrencyConverterServiceImpl recordLog compute={}", JSONObject.toJSONString(moneyExchangeRateParam));
        MoneyExchangeRateResponse compute = MoneyExchangeRateService.compute(moneyExchangeRateParam);
        log.info("CurrencyConverterServiceImpl recordLog compute={}", JSONObject.toJSONString(compute));

        currencyConversion.setFromCurrency(exchangeRateDto.getCurrentCurrency());
        currencyConversion.setToCurrency(exchangeRateDto.getTargetCurrency());
        currencyConversion.setCurrentRate(new BigDecimal(7.31));
        currencyConversion.setTargetCurrencyMoney(compute.getTargetCurrencyMoney().getAmount());
        currencyConversion.setLastUpdate(222L);
        currencyConversion.setTargetRate(BigDecimal.ONE.divide(currencyConversion.getCurrentRate(), 4, RoundingMode.HALF_UP));

        return currencyConversion;
    }
}
